home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / pnm / oct-utils.cc < prev    next >
C/C++ Source or Header  |  1997-02-01  |  3KB  |  140 lines

  1. /*
  2. *******************************************************************************
  3. ** Interface functions to Octave                                             **
  4. ** (c) 1997, Klaus Gebhardt                                                  **
  5. *******************************************************************************
  6. */
  7.  
  8. #include "oct-img.h"
  9.  
  10. extern "C"
  11. {
  12.   UCHAR **malloc_uchar_matrix (UINT, UINT);
  13.   INT   **malloc_int_matrix   (UINT, UINT);
  14. }
  15.  
  16.  
  17. UCHAR **gif_colormap (octave_value map, INT *colors, BOOL *grey)
  18. {
  19.   UCHAR **cmap;
  20.   INT i;
  21.  
  22.   if (grey)  *grey = OCT_GREY;
  23.   *colors = (INT) map.rows ();
  24.  
  25.   if (!map.is_matrix_type () || !map.is_real_type () ||
  26.       (((unsigned long) map.columns ()) != 3) || ((*colors) < 1))
  27.     {
  28.       ::error ("gif_colormap: colormap must be a real N x 3 matrix");
  29.       return NULL;
  30.     }
  31.  
  32.   Matrix Map = map.matrix_value ();
  33.  
  34.   cmap = malloc_uchar_matrix (3, (*colors));
  35.   if (cmap)
  36.     {
  37.       for (i = 0; i < (*colors); i++)
  38.     {
  39.       cmap[0][i] = (UCHAR) (min (max (Map (i, 0), 0), 1) * 255.);
  40.       cmap[1][i] = (UCHAR) (min (max (Map (i, 1), 0), 1) * 255.);
  41.       cmap[2][i] = (UCHAR) (min (max (Map (i, 2), 0), 1) * 255.);
  42.       if (grey)
  43.         if ((cmap[0][i] != cmap[1][i]) || (cmap[0][i] != cmap[2][i]))
  44.           *grey = OCT_RGB;
  45.     }
  46.  
  47.       if (grey && ((*grey) == OCT_GREY) && ((*colors) < 3))
  48.     {
  49.       if (((*colors) == 1) && ((cmap[0][0] == 0) || (cmap[0][0] == 255)))
  50.         *grey = OCT_BLACKWHITE;
  51.       else
  52.         {
  53.           if ((cmap[0][0]*cmap[0][1] == 0) &&
  54.           (cmap[0][0]+cmap[0][1] == 255))
  55.         *grey = OCT_BLACKWHITE;
  56.         }
  57.     }
  58.     }
  59.   else
  60.     ::error ("gif_colormap: out of memory");
  61.  
  62.   return cmap;
  63. }
  64.  
  65.  
  66. Matrix oct_colormap (UCHAR **cmap, INT col_min, INT col_max)
  67. {
  68.   INT i;
  69.  
  70.   Matrix Map (col_max - col_min + 1, 3);
  71.  
  72.   for (i = col_min; i < col_max + 1; i++)
  73.     {
  74.       Map (i - col_min, 0) = ((OCTAVE) cmap[0][i]) / 255.;
  75.       Map (i - col_min, 1) = ((OCTAVE) cmap[1][i]) / 255.;
  76.       Map (i - col_min, 2) = ((OCTAVE) cmap[2][i]) / 255.;
  77.     }
  78.  
  79.   free (cmap);
  80.   return Map;
  81. }
  82.  
  83.  
  84. INT **gif_pixels (octave_value img, INT colors, UINT *nr, UINT *nc)
  85. {
  86.   INT **x;
  87.   INT c;
  88.   UINT i, j;
  89.  
  90.   *nr = (UINT) img.rows ();
  91.   *nc = (UINT) img.columns ();
  92.  
  93.   if (colors < 1)
  94.     {
  95.       ::error ("gif_pixels: number of colors must be positive");
  96.       return NULL;
  97.     }
  98.  
  99.   if (!img.is_matrix_type () || !img.is_real_type () ||
  100.       ((*nc) < 1) || ((*nr) < 1))
  101.     {
  102.       ::error ("gif_pixels: img must be a real matrix");
  103.       return NULL;
  104.     }
  105.  
  106.   Matrix X = img.matrix_value ();
  107.  
  108.   x = malloc_int_matrix ((*nr), (*nc));
  109.   if (x)
  110.     {
  111.       for (i = 0; i < (*nr); i++)
  112.     {
  113.       for (j = 0; j < (*nc); j++)
  114.         {
  115.           c = min (max ((INT) (X (i, j) - 1.), 0), colors - 1);
  116.           x[i][j] = c;
  117.         }
  118.     }
  119.     }
  120.   else
  121.     ::error ("gif_pixels: out of memory");
  122.  
  123.   return x;
  124. }
  125.  
  126.  
  127. Matrix oct_pixels (INT **x, INT col_min, UINT nr, UINT nc)
  128. {
  129.   UINT i, j;
  130.  
  131.   Matrix X (nr, nc);
  132.  
  133.   for (i = 0; i < nr; i++)
  134.     for (j = 0; j < nc; j++)
  135.       X (i, j) = ((double) (x[i][j])) - ((double) col_min) + 1.;
  136.  
  137.   free (x);
  138.   return X;
  139. }
  140.